/*
package com.mata.ddtalk.utils;

import com.alibaba.fastjson.JSONObject;
import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiDepartmentListRequest;
import com.dingtalk.api.request.OapiGetJsapiTicketRequest;
import com.dingtalk.api.request.OapiGettokenRequest;
import com.dingtalk.api.request.OapiUserSimplelistRequest;
import com.dingtalk.api.response.OapiDepartmentListResponse;
import com.dingtalk.api.response.OapiGetJsapiTicketResponse;
import com.dingtalk.api.response.OapiGettokenResponse;
import com.dingtalk.api.response.OapiUserSimplelistResponse;
import com.mata.ddtalk.domain.ServiceResult;
import com.taobao.api.ApiException;
import org.springframework.beans.factory.annotation.Value;

*/
/**
 * t
 *//*

public class DingUtil {

    private final static String APPKEY ="dingkkl6d1gh1mwjdqfg";

    private final static String APPSECRET="To3UK64Rt31ZZ6hvXDISuII_01IQF1Bm7wEa7T-G0Vx6U_xYbNfOIKarS7VX80mw";

//    @Value("${dd.appKey}")
//    public String appkey;
//
//    @Value("${dd.appSecret}")
//    public String appSecret;

    //获取token
    public static String getToken (){
        Object object = LocalCacheClient.get("access_token");
        if(object != null){
            return object.toString();
        }
        DefaultDingTalkClient client = new
                DefaultDingTalkClient("https://oapi.dingtalk.com/gettoken");
        OapiGettokenRequest request = new OapiGettokenRequest();
        request.setAppkey(APPKEY);
        request.setAppsecret(APPSECRET);
        request.setHttpMethod("GET");
        try {
            OapiGettokenResponse response = client.execute(request);
            LocalCacheClient.set("access_token", response.getAccessToken(),7200*1000);
            return response.getAccessToken();
        } catch (ApiException e) {
            e.printStackTrace();
        }
        return null;
    }

    */
/**
     * 获取JSTicket, 用于js的签名计算
     * 正常的情况下，jsapi_ticket的有效期为7200秒，所以开发者需要在某个地方设计一个定时器，定期去更新jsapi_ticket
     *
     * @return jsTicket或错误信息
     *//*

    public ServiceResult<String> getJsTicket() {
        // 从持久化存储中读取
        String ticket = getFromCache("jsticket", "ticket");
        if (ticket != null) {
            return ServiceResult.success(ticket);
        }

        String accessToken;
        ServiceResult<String> tokenSr = getToken();
        if (!tokenSr.isSuccess()) {
            return ServiceResult.failure(tokenSr.getCode(), tokenSr.getMessage());
        }
        accessToken = tokenSr.getResult();

        DefaultDingTalkClient client = new DefaultDingTalkClient(URL_GET_JSTICKET);
        OapiGetJsapiTicketRequest request = new OapiGetJsapiTicketRequest();
        OapiGetJsapiTicketResponse response;

        request.setHttpMethod("GET");

        try {
            response = client.execute(request, accessToken);
        } catch (ApiException e) {
            log.error("getAccessToken failed", e);
            return ServiceResult.failure(e.getErrCode(), e.getErrMsg());
        }

        if (!response.isSuccess()) {
            return ServiceResult.failure(response.getErrorCode(), response.getErrmsg());
        }
        ticket = response.getTicket();
        putToCache("jsticket", "ticket", ticket);
        return ServiceResult.success(ticket);
    }

    */
/**
     * 模拟从持久化存储中获取token并检查是否已过期
     *
     * @param section 存储key
     * @param field   token字段名
     * @return token值 或 null (过期或未查到)
     *//*

    private String getFromCache(String section, String field) {
        JSONObject o = (JSONObject) FileUtil.getValue(section, appConfig.getAppKey());
        if (o != null && System.currentTimeMillis() - o.getLong("begin_time") <= CACHE_TTL) {
            return o.getString(field);
        }
        return null;
    }

    private void putToCache(String section, String field, String value) {
        JSONObject fieldObj = new JSONObject(2);
        fieldObj.put(field, value);
        fieldObj.put("begin_time", System.currentTimeMillis());
        JSONObject wrapperObj = new JSONObject(1);
        wrapperObj.put(appConfig.getAppKey(), fieldObj);

        FileUtil.write2File(wrapperObj, section);
    }

    //获取部门列表
    public static Object getDepartment(){
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/department/list");
        OapiDepartmentListRequest request = new OapiDepartmentListRequest();
        //获取根部门下所有部门列表  根部门的部门id为1
        request.setId("1");
        request.setHttpMethod("GET");

        try {
            OapiDepartmentListResponse response = client.execute(request,DingUtil.getToken());
            return response;
        } catch (ApiException e) {
            e.printStackTrace();
        }
        return null;
    }

    //获取部门下的所有用户列表
    public static Object getDepartmentUser(Long departmentId){
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/user/simplelist");
        OapiUserSimplelistRequest request = new OapiUserSimplelistRequest();
        request.setDepartmentId(departmentId);
        request.setOffset(0L);
        request.setSize(10L);
        request.setHttpMethod("GET");

        try {
            OapiUserSimplelistResponse response = client.execute(request, DingUtil.getToken());
            return response;
        } catch (ApiException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }
}
*/
